<?php
  class BuildGame {
     
  function deleteDirectory($dir) {
    if (!file_exists($dir)) return true;
    if (!is_dir($dir) || is_link($dir)) return unlink($dir);
        foreach (scandir($dir) as $item) {
            if ($item == '.' || $item == '..') continue;
            if (!deleteDirectory($dir . "/" . $item)) {
                chmod($dir . "/" . $item, 0777);
                if (!deleteDirectory($dir . "/" . $item)) return false;
            };
        }
        return rmdir($dir);
    } 
    function deleteFolder($dir) {
        if (!file_exists($dir)) return true;
        $it = new RecursiveDirectoryIterator($dir);
        $files = new RecursiveIteratorIterator($it,
                     RecursiveIteratorIterator::CHILD_FIRST);
        foreach($files as $file) {
            if ($file->getFilename() === '.' || $file->getFilename() === '..') {
                continue;
            }
            if ($file->isDir()){
                rmdir($file->getRealPath());
            } else {
                unlink($file->getRealPath());
            }
        }
        return rmdir($dir);
    } 
    
   function createFileRefcode($fileRefcode,$refcode)
   {
         //$myFile = "refcode.txt";
         $fh = fopen($fileRefcode, 'w') or die("can't open file");
         $stringData = $refcode."\n";
         fwrite($fh, $stringData);
         fclose($fh);
   }
   function changeExtensionJadToJar($fileJad){
       $arr =array();
       $arr=explode(".",$fileJad);
       $arr[count($arr)-1]="jar";
       $fileJar = implode(".",$arr);
       return $fileJar;
   }
   function extractRefcode($fileZip,$fileExtract,$extractTo){
        $zip = new ZipArchive;
        $res = $zip->open($fileZip);
        if ($res === TRUE) {
            $zip->extractTo($extractTo, array($fileExtract));
            $zip->close();
        } else {
        }
   }

   function addFileRefcode($fileIpa,$fileRefcode,$fileRefcodeDes){
            $zip = new ZipArchive;
            if ($zip->open($fileIpa) === TRUE) {
                $zip->addFile($fileRefcode, $fileRefcodeDes);
                $zip->close();
            } else {
            }
   }        
           
  function extractFileZip($fileZip,$extractTo){
    $zip = new ZipArchive;
    if ($zip->open($fileZip) === true) {
        for($i = 0; $i < $zip->numFiles; $i++) {
            $zip->extractTo($extractTo, array($zip->getNameIndex($i)));
        }
        $zip->close();
    }
  }

   function downloadFile($url_file,$fileName,$fileExtension){
           header("Pragma: public");
            if($fileExtension=="jad"){
                header("Content-type: text/vnd.sun.j2me.app-descriptor");    
            } else if($fileExtension=="jar"){
                header("Content-type: application/java-archive;");    
            } else if($fileExtension=="apk"){
                header("Content-type: application/vnd.android.package-archive");        
            }

            header('Content-Disposition: attachment; filename="' . $fileName . "." . $fileExtension . '"');                
            ob_clean();
            flush();
            $content = Common::cUrl($url_file);
            echo $content;
            exit();
   }  
   
   function changeLineFile($file,$line,$line_content){
        $handle = @fopen($file, "r+");
        $contentFileNew="";
          $i = 1;
        if ($handle) {
            while (($buffer = fgets($handle, 4096)) !== false) {
                if($i==$line){
                 $contentFileNew .= trim($line_content)."\n"; 
                }
                else{
                    $contentFileNew .= $buffer;
                }
                $i++;
            }

            if (!feof($handle)) {
                echo "Error: unexpected fgets() fail\n";
            }
            file_put_contents($file, $contentFileNew);
            fclose($handle);
        }
   }  
  

   function buildFileJava($fileJarSource,$folderJarDes,$appId,$refcode,$filename,$username,$type){
        if (!file_exists($folderJarDes)) {
            Common::recursive_mkdir($folderJarDes, 0755);
         }
         $userInfo = User::getUserId($refcode);
         $userId =  $userInfo['id'];
         switch($appId){  
            case 2 :
            {
                copy($fileJarSource,$folderJarDes.$filename);
                BuildGame::extractRefcode($fileJarSource,"META-INF/MANIFEST.MF",$folderJarDes);
                BuildGame::changeLineFile($folderJarDes."META-INF/MANIFEST.MF",4,"RefCode: ".$refcode);
                BuildGame::addFileRefcode($folderJarDes.$filename,$folderJarDes."META-INF/MANIFEST.MF","META-INF/MANIFEST.MF")    ;
                @unlink($folderJarDes."META-INF/MANIFEST.MF");
                break;
            }
             case 4 :
            {
                copy($fileJarSource,$folderJarDes.$filename);
                BuildGame::extractRefcode($fileJarSource,"META-INF/MANIFEST.MF",$folderJarDes);
                BuildGame::changeLineFile($folderJarDes."META-INF/MANIFEST.MF",17,"RefCode: ".$refcode);
                BuildGame::addFileRefcode($folderJarDes.$filename,$folderJarDes."META-INF/MANIFEST.MF","META-INF/MANIFEST.MF")    ;
                @unlink($folderJarDes."META-INF/MANIFEST.MF");
                break;
            }
            case 8 :
            {   
                copy($fileJarSource,$folderJarDes.$filename);
                BuildGame::extractRefcode($fileJarSource,"META-INF/MANIFEST.MF",$folderJarDes);
                BuildGame::changeLineFile($folderJarDes."META-INF/MANIFEST.MF",14,"Channel: 236-".$refcode);
                BuildGame::addFileRefcode($folderJarDes.$filename,$folderJarDes."META-INF/MANIFEST.MF","META-INF/MANIFEST.MF");
                @unlink($folderJarDes."META-INF/MANIFEST.MF");
                break;
            }
            case 5 :
            {
                copy($fileJarSource,$folderJarDes.$filename);
                BuildGame::extractRefcode($fileJarSource,"META-INF/MANIFEST.MF",$folderJarDes);
                BuildGame::changeLineFile($folderJarDes."META-INF/MANIFEST.MF",11,"RefCode: ".$refcode);
                BuildGame::addFileRefcode($folderJarDes.$filename,$folderJarDes."META-INF/MANIFEST.MF","META-INF/MANIFEST.MF")    ;
                @unlink($folderJarDes."META-INF/MANIFEST.MF");
                break;
            }
            case 7 :
            {
                copy($fileJarSource,$folderJarDes.$filename);
                BuildGame::extractRefcode($fileJarSource,"META-INF/MANIFEST.MF",$folderJarDes);
                BuildGame::changeLineFile($folderJarDes."META-INF/MANIFEST.MF",13,"Refcode: ".$refcode);
                BuildGame::addFileRefcode($folderJarDes.$filename,$folderJarDes."META-INF/MANIFEST.MF","META-INF/MANIFEST.MF")    ;
                @unlink($folderJarDes."META-INF/MANIFEST.MF");
                break;
            }
            case 18 :{
                copy($fileJarSource,$folderJarDes.$filename);
                BuildGame::extractRefcode($fileJarSource,"Vintelco/channel.txt",$folderJarDes);
                BuildGame::extractRefcode($fileJarSource,"Vintelco/url.txt",$folderJarDes);
                BuildGame::changeLineFile($folderJarDes."Vintelco/channel.txt",1,$userId);
                BuildGame::changeLineFile($folderJarDes."Vintelco/url.txt",1,"http://mtop.vn/upload/user/".$username."/game/".$filename);
                BuildGame::addFileRefcode($folderJarDes.$filename,$folderJarDes."Vintelco/channel.txt","Vintelco/channel.txt")    ;
                BuildGame::addFileRefcode($folderJarDes.$filename,$folderJarDes."Vintelco/url.txt","Vintelco/url.txt")    ;
                @unlink($folderJarDes."Vintelco/url.txt");
                @unlink($folderJarDes."Vintelco/channel.txt");
                break;
            }
            case 6 :
            {
                copy($fileJarSource,$folderJarDes.$filename);
                BuildGame::extractRefcode($fileJarSource,"Vintelcom/channel.txt",$folderJarDes);
                BuildGame::extractRefcode($fileJarSource,"Vintelcom/url.txt",$folderJarDes);
                BuildGame::changeLineFile($folderJarDes."Vintelcom/channel.txt",1,$userId);
                BuildGame::changeLineFile($folderJarDes."Vintelcom/url.txt",1,"http://mtop.vn/upload/user/".$username."/game/".$filename);
                BuildGame::addFileRefcode($folderJarDes.$filename,$folderJarDes."Vintelcom/channel.txt","Vintelcom/channel.txt")    ;
                BuildGame::addFileRefcode($folderJarDes.$filename,$folderJarDes."Vintelcom/url.txt","Vintelcom/url.txt")    ;
                @unlink($folderJarDes."Vintelcom/url.txt");
                @unlink($folderJarDes."Vintelcom/channel.txt");
                break;
            }
            case 11 :
            {
                copy($fileJarSource,$folderJarDes.$filename);
                BuildGame::extractRefcode($fileJarSource,"META-INF/channel.txt",$folderJarDes);
                BuildGame::extractRefcode($fileJarSource,"META-INF/url.txt",$folderJarDes);
                BuildGame::changeLineFile($folderJarDes."META-INF/channel.txt",1,$userId);
                BuildGame::changeLineFile($folderJarDes."META-INF/url.txt",1,"http://mtop.vn/upload/user/".$username."/game/".$filename);
                BuildGame::addFileRefcode($folderJarDes.$filename,$folderJarDes."META-INF/channel.txt","META-INF/channel.txt")    ;
                BuildGame::addFileRefcode($folderJarDes.$filename,$folderJarDes."META-INF/url.txt","META-INF/url.txt")    ;
                @unlink($folderJarDes."META-INF/url.txt");
                @unlink($folderJarDes."META-INF/channel.txt");
                break;
            }
            
         }
    }
    function buildFileJad($fileJarSource,$folderJarDes,$appId,$refcode,$filename,$username,$type){
        if (!file_exists($folderJarDes)) {
            Common::recursive_mkdir($folderJarDes, 0755);
         }
         switch($appId){  
            case 2 :
            {   
                copy($fileJarSource,$folderJarDes.$filename);
                $filenameJar = BuildGame::changeExtensionJadToJar($filename);
                BuildGame::changeLineFile($folderJarDes.$filename,9,"MIDlet-Jar-URL: "."http://mtop.vn/upload/user/".$username."/game/".$filenameJar);
                BuildGame::changeLineFile($folderJarDes.$filename,4,"RefCode: ".$refcode);
                break;
            }
             case 4 :
            {   
                copy($fileJarSource,$folderJarDes.$filename);
                $filenameJar = BuildGame::changeExtensionJadToJar($filename);
                BuildGame::changeLineFile($folderJarDes.$filename,5,"MIDlet-Jar-URL: "."http://mtop.vn/upload/user/".$username."/game/".$filenameJar);
                BuildGame::changeLineFile($folderJarDes.$filename,15,"RefCode: ".$refcode);
                break;
            }
            case 18 :
            {   
                copy($fileJarSource,$folderJarDes.$filename);
                $filenameJar = BuildGame::changeExtensionJadToJar($filename);
                BuildGame::changeLineFile($folderJarDes.$filename,18,"MIDlet-Jar-URL: "."http://mtop.vn/upload/user/".$username."/game/".$filenameJar);
               
                break;
            }
            case 8 :
            {   
                copy($fileJarSource,$folderJarDes.$filename);
                $filenameJar = BuildGame::changeExtensionJadToJar($filename);
                BuildGame::changeLineFile($folderJarDes.$filename,3,"MIDlet-Jar-URL: "."http://mtop.vn/upload/user/".$username."/game/".$filenameJar);
                BuildGame::changeLineFile($folderJarDes.$filename,13,"Channel: 236-".$refcode);
                break;
            }
            case 5 :
            {   
                copy($fileJarSource,$folderJarDes.$filename);
                $filenameJar = BuildGame::changeExtensionJadToJar($filename);
                BuildGame::changeLineFile($folderJarDes.$filename,5,"MIDlet-Jar-URL: "."http://mtop.vn/upload/user/".$username."/game/".$filenameJar);
                BuildGame::changeLineFile($folderJarDes.$filename,14,"RefCode: ".$refcode);
                break;
            }
            case 7 :
            {   
                copy($fileJarSource,$folderJarDes.$filename);
                $filenameJar = BuildGame::changeExtensionJadToJar($filename);
                BuildGame::changeLineFile($folderJarDes.$filename,6,"MIDlet-Jar-URL: "."http://mtop.vn/upload/user/".$username."/game/".$filenameJar);
                BuildGame::changeLineFile($folderJarDes.$filename,16,"RefCode: ".$refcode);
                break;
            }
         }
    }
    
     function buildAppIPA($fileSource,$folderDes,$appId,$filename,$username,$refcode){
         if (!file_exists($folderDes)) {
            Common::recursive_mkdir($folderDes, 0755);
         }   
         $userInfo = User::getUserId($refcode);
         $userId =  $userInfo['id'];
         switch($appId){  
            case 10 :
            {   
                /* Build file ipa*/
                copy($fileSource,$folderDes.$filename);
                BuildGame::extractRefcode($fileSource,"Payload/tsz_appstore_yuenan.app/config.properties",$folderDes);
                BuildGame::changeLineFile($folderDes."Payload/tsz_appstore_yuenan.app/config.properties",10,"Channel=52|".$refcode."|1.2.0");
                BuildGame::addFileRefcode($folderDes.$filename,$folderDes."Payload/tsz_appstore_yuenan.app/config.properties","Payload/tsz_appstore_yuenan.app/config.properties");
                unlink($folderDes."Payload/tsz_appstore_yuenan.app/config.properties");
                /* Build file plist*/
                $basename = basename($filename,".ipa");
                $arr=array();
                $arr = explode("/",$fileSource);
                $arr[count($arr)-1]= $basename.".plist";
                $fileSourcePlist = implode("/",$arr);
                copy($fileSourcePlist,$folderDes.$basename.".plist");
                BuildGame::changeLineFile($folderDes.$basename.".plist",14,"<string>http://mtop.vn/upload/user/".$username."/game/".$filename."</string>");
                break;
            }
             case 11 :
            {   
                /* Build file ipa*/
                copy($fileSource,$folderDes.$filename);
                $fileRefcode = $folderDes."refcode.txt";
                BuildGame::createFileRefcode($fileRefcode,$userId) ;
                BuildGame::addFileRefcode($folderDes.$filename,$fileRefcode,"Payload/VuaBai.app/refcode.txt");
                unlink($fileRefcode);
                /* Build file plist*/
                $basename = basename($filename,".ipa");
                $arr=array();
                $arr = explode("/",$fileSource);
                $arr[count($arr)-1]= $basename.".plist";
                $fileSourcePlist = implode("/",$arr);
                copy($fileSourcePlist,$folderDes.$basename.".plist");
                BuildGame::changeLineFile($folderDes.$basename.".plist",14,"<string>http://mtop.vn/upload/user/".$username."/game/".$filename."</string>");
                break;
            }
            case 999:
            {
                copy($fileSource,$folderDes.$filename);
                $fileRefcode = $folderDes."refcode.txt";
                BuildGame::createFileRefcode($fileRefcode,$refcode) ;
                BuildGame::addFileRefcode($folderDes.$filename,$fileRefcode,$fileRefcode);  
              break;
            }
         }
    }
    
    
    function buildAppAPK($fileSource,$folderDes,$appId,$filename,$username,$refcode){
        
        if (!file_exists($folderDes)) {
            Common::recursive_mkdir($folderDes, 0755);
         }
         $userInfo = User::getUserId($refcode);
         $userId =  $userInfo['id'];
        //asign apk
        /*$pathApkTool = "D:/wamp/www/appstore/kichhoat_apk/EasyApkDisassembler/SYSTEM/signapk.jar";
        $pathApkTool1 = "D:/wamp/www/appstore/kichhoat_apk/EasyApkDisassembler/SYSTEM/testkey.x509.pem";
        $pathApkTool2 = "D:/wamp/www/appstore/kichhoat_apk/EasyApkDisassembler/SYSTEM/testkey.pk8"; 
        $pathApkTool3 = "D:/wamp/www/appstore/kichhoat_apk/EasyApkDisassembler/SYSTEM.EAD/apktool.jar";*/
        
        $pathApkTool = "/home/mtop/domains/mtop.vn/public_html/kichhoat_apk/EasyApkDisassembler/SYSTEM/signapk.jar";
        $pathApkTool1 = "/home/mtop/domains/mtop.vn/public_html/kichhoat_apk/EasyApkDisassembler/SYSTEM/testkey.x509.pem";
        $pathApkTool2 = "/home/mtop/domains/mtop.vn/public_html/kichhoat_apk/EasyApkDisassembler/SYSTEM/testkey.pk8"; 
        $pathApkTool3 = "/home/mtop/domains/mtop.vn/public_html/kichhoat_apk/EasyApkDisassembler/SYSTEM/apktool.jar"; 
         switch($appId){  
            case 2 :
            { 
                copy($fileSource,$folderDes.$filename);
                BuildGame::extractRefcode($fileSource,"jad.MF",$folderDes);
                BuildGame::changeLineFile($folderDes."jad.MF",11,"Refcode: ".$refcode);
                BuildGame::addFileRefcode($folderDes.$filename,$folderDes."jad.MF","jad.MF");
                unlink($folderDes."jad.MF");
                break;
            }
             case 8 :
            { 
                copy($fileSource,$folderDes.$filename);
                $fileRefcode = $folderDes."Channel";
                BuildGame::createFileRefcode($fileRefcode,"236-".$refcode) ;
                BuildGame::addFileRefcode($folderDes.$filename,$fileRefcode,"META-INF/Channel")    ;
                unlink($fileRefcode);
                break;
            }
            case 5 :
            { 
                copy($fileSource,$folderDes.$filename);
                BuildGame::extractRefcode($fileSource,"META-INF/jad.properties",$folderDes);
                BuildGame::changeLineFile($folderDes."META-INF/jad.properties",12,"Refcode: ".$refcode);
                BuildGame::addFileRefcode($folderDes.$filename,$folderDes."META-INF/jad.properties","META-INF/jad.properties");
                unlink($folderDes."META-INF/jad.properties");
                break;
            }
            case 7 :
            { 
                copy($fileSource,$folderDes.$filename);
                BuildGame::extractRefcode($fileSource,"META-INF/config.properties",$folderDes);
                BuildGame::changeLineFile($folderDes."META-INF/config.properties",6,"RefCode=".$refcode);
                BuildGame::addFileRefcode($folderDes.$filename,$folderDes."META-INF/config.properties","META-INF/config.properties");
                unlink($folderDes."META-INF/config.properties");
                break;
            }
            case 10 :
            {   
                copy($fileSource,$folderDes.$filename);
                BuildGame::extractRefcode($fileSource,"META-INF/config.properties",$folderDes);
                BuildGame::changeLineFile($folderDes."META-INF/config.properties",1,"Channel=52|".$refcode);
                BuildGame::addFileRefcode($folderDes.$filename,$folderDes."META-INF/config.properties","META-INF/config.properties");
                unlink($folderDes."META-INF/config.properties");
                break;
            }
            case 11 :
            { 
                copy($fileSource,$folderDes.$filename);
                $fileRefcode = $folderDes."ref_code.txt";
                BuildGame::createFileRefcode($fileRefcode,$userId) ;
                BuildGame::addFileRefcode($folderDes.$filename,$fileRefcode,"assets/ref_code.txt")    ;
                unlink($fileRefcode);
                break;
            }
            case 15 :
            { 
                copy($fileSource,$folderDes.$filename);
                if(@chdir($folderDes."project") == false){
                    @mkdir($folderDes."project", 0755, true);
                }
                $command ="java -jar ".$pathApkTool3." d -f  ".$folderDes.$filename." ".$folderDes."project/dis_".$filename ;
                @exec($command, $output, $response);
                BuildFile::changeLineFile($folderDes."project/dis_".$filename."/assets/data.txt",1,$refcode);
                
               $command ="java -jar ".$pathApkTool3."   -q b -a /home/mtop/domains/mtop.vn/public_html/kichhoat_apk/EasyApkDisassembler/SYSTEM/aapt ".$folderDes."project/dis_".$filename ;
                @exec($command, $output, $response);
                $result=copy($folderDes."project/dis_".$filename."/dist/".$filename,$folderDes.$filename);
                if($result){
                  $kq = BuildGame::deleteFolder($folderDes."project/dis_".$filename);  
                }
                break;
            }
            case 16 :
            { 
                copy($fileSource,$folderDes.$filename);
                if(@chdir($folderDes."project") == false){
                    @mkdir($folderDes."project", 0755, true);
                }
                $command ="java -jar ".$pathApkTool3." d -f  ".$folderDes.$filename." ".$folderDes."project/dis_".$filename ;
                @exec($command, $output, $response);
                BuildFile::changeLineFile($folderDes."project/dis_".$filename."/assets/data.txt",1,$refcode);
                
               $command ="java -jar ".$pathApkTool3."   -q b -a /home/mtop/domains/mtop.vn/public_html/kichhoat_apk/EasyApkDisassembler/SYSTEM/aapt ".$folderDes."project/dis_".$filename ;
                @exec($command, $output, $response);
                $result=copy($folderDes."project/dis_".$filename."/dist/".$filename,$folderDes.$filename);
                if($result){
                  $kq = BuildGame::deleteFolder($folderDes."project/dis_".$filename);    
                }
                break;
            }
            case 17 :
            { 
                copy($fileSource,$folderDes.$filename);
                if(@chdir($folderDes."project") == false){
                    @mkdir($folderDes."project", 0755, true);
                }
                $command ="java -jar ".$pathApkTool3." d -f  ".$folderDes.$filename." ".$folderDes."project/dis_".$filename ;
                @exec($command, $output, $response);
                BuildFile::changeLineFile($folderDes."project/dis_".$filename."/assets/data.txt",1,$refcode);
                
               $command ="java -jar ".$pathApkTool3."   -q b -a /home/mtop/domains/mtop.vn/public_html/kichhoat_apk/EasyApkDisassembler/SYSTEM/aapt ".$folderDes."project/dis_".$filename ;
                @exec($command, $output, $response);
                $result=copy($folderDes."project/dis_".$filename."/dist/".$filename,$folderDes.$filename);
                if($result){
                  $kq = BuildGame::deleteFolder($folderDes."project/dis_".$filename);  
                }
                break;
            }
            
         }
        
        $file1 = $folderDes.$filename; 
        $file2= $folderDes."asinged_".$filename; 
        $command ="java -jar ".$pathApkTool." ".$pathApkTool1. " ".$pathApkTool2." ".$file1." ".$file2."   ";
        @exec($command, $output, $response);
        rename($file2,$file1);
    }
  }
?>
